#include <aknipfed.h>
#include <akncontext.h>
#include <akntabgrp.h>
#include <aknnavide.h>
#include <coemain.h>
#include <AknViewAppUi.h>
#include <akntextsettingpage.h>
#include <aknmfnesettingpage.h> 
#include <aknpopupsettingpage.h>
#include <aknmessagequerydialog.h>
#include <akncheckboxsettingpage.h> 
#include <aknvolumesettingpage.h> 

#include <MrLockUi.rsg>

#include "SettingsBaseView.h"
#include "Application.h"
#include "AppUi.h"
#include "MrLock.hrh"
#include "SettingsListBox.h"
#include "Logger.h"

CSettingsBaseView::CSettingsBaseView()
{}

CSettingsBaseView::~CSettingsBaseView()
{}

CAknDurationSettingPage* CSettingsBaseView::CreateDurationSettingPageL(const TDesC& aTitle, TTimeIntervalSeconds& aValue, TInt aInd)
{
	CAknDurationSettingPage* page = new (ELeave) CAknDurationSettingPage(R_DURATION_SETTING_PAGE2, aValue);

	CleanupStack::PushL(page);//+page
	if (aInd != KErrNotFound)
		page->SetSettingNumberL(aInd+1);
	page->SetSettingTextL(aTitle);
	CleanupStack::Pop();//-page
	return page;
}

CAknTextSettingPage* CSettingsBaseView::CreateTextSettingPageL(const TDesC& aTitle, TDes& aValue, TInt aInd)
{
	CAknTextSettingPage* page = new (ELeave) CAknTextSettingPage(R_TEXT_SETTING_PAGE, aValue);
	CleanupStack::PushL(page);//+page
	if (aInd != KErrNotFound)
		page->SetSettingNumberL(aInd+1);
	page->SetSettingTextL(aTitle);
	CleanupStack::Pop();//-page
	return page;
}

void CSettingsBaseView::LoadIconsL()
{
}

void CSettingsBaseView::CreateListBoxL()
{
	delete iListBox;
	iListBox = NULL;

	iListBox = new (ELeave) CSettingsListBox(this);
	iListBox->ConstructL(NULL, 0);
	iListBox->SetRect(ClientRect());
	iListBox->SetMopParent(this);
	iItems = (CDesCArray*) (iListBox->Model()->ItemTextArray());
	LoadIconsL();
	LoadItemsL();
	iListBox->SetCurrentItemIndex(iLastInd);
	iListBox->HandleItemAdditionL();
	iListBox->CreateScrollBarFrameL();
	iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EAuto, CEikScrollBarFrame::EAuto);
}

void CSettingsBaseView::DoActivateL(const TVwsViewId& aPrevViewId, TUid /*aCustomMessageId*/, const TDesC8& /*aCustomMessage*/)
{
	CreateListBoxL();
	AppUi()->AddToStackL(*this, iListBox);
	iListBox->ActivateL();
}

void CSettingsBaseView::DoDeactivate()
{
	AppUi()->RemoveFromStack(iListBox);

	delete iListBox;
	iListBox = NULL;
}

void CSettingsBaseView::HandleCommandL(TInt aCommand)
{
	AppUi()->HandleCommandL(aCommand);
}

void CSettingsBaseView::SelectedIndexChangedL(TInt aInd)
{
	iLastInd = aInd;
}

TPtrC CSettingsBaseView::Title(TInt aInd)
{
	TPtrC title = iItems->operator[](aInd).Mid(1);
	TInt ind = title.Locate('\t');
	title.Set(title.Left(ind));	
	return title;
}